home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Tools / Win95 Secrets / SETUP.Z / PHYS.C < prev    next >
Encoding:
C/C++ Source or Header  |  1995-07-19  |  9.5 KB  |  315 lines

  1. //==================================
  2. // PHYS - Matt Pietrek 1995
  3. // FILE: PHYS.C
  4. //==================================
  5.  
  6. #include <windows.h>
  7. #include <stdio.h>
  8. #include <conio.h>
  9.  
  10. DWORD GetPhysicalAddrFromLinear(DWORD linear);  // Prototype helper funcs
  11. WORD __stdcall GetRing0Callgate( DWORD addr, unsigned cParams );
  12. BOOL __stdcall FreeRing0Callgate( WORD callgate );
  13. DWORD _GetPhysicalAddrFromLinear(DWORD linear);     // ASM version
  14. DWORD _GetPageAttributes(DWORD linear);     // ASM version
  15.  
  16. HMODULE WINAPI LoadLibrary16( LPCSTR lpLibFileName );
  17. BOOL WINAPI FreeLibrary16( HINSTANCE hLibModule );
  18.  
  19. void ShowPhysicalPages(void);
  20. void CreateSharedMemoryRegion(void);
  21. void DeleteSharedMemoryRegion(void);
  22. void ModifyCodePage(void);
  23. DWORD Get_KRNL386_DGROUP_LinearAddress(void);
  24. PSTR GetPageAttributesAsString(DWORD linear);
  25.  
  26. BOOL FirstInstance = TRUE;
  27. WORD callgate1 = 0;
  28. WORD callgate2 = 0;
  29. PBYTE PMemMapFileRegion;
  30.  
  31. #pragma data_seg("SHAREDAT")        // Declare a variable in a shared
  32. int MySharedSectionVariable = 0;    // Section.  The variable must be
  33. #pragma data_seg()                  // initialized for the linker to put it
  34.                                     // in the specified section
  35.  
  36. int main()
  37. {
  38.     CreateSharedMemoryRegion();
  39.  
  40.     if ( FirstInstance )
  41.         printf("***** FIRST INSTANCE *****\n");
  42.     else
  43.         printf("***** SECONDARY INSTANCE *****\n");
  44.  
  45.     ShowPhysicalPages();
  46.  
  47.     printf("Press any key...\n");
  48.     getch();
  49.  
  50.     if ( FirstInstance )
  51.     {
  52.         printf("\nNow modifying the code page\n");
  53.         ModifyCodePage();
  54.         ShowPhysicalPages();
  55.     }
  56.     
  57.     FreeRing0Callgate( callgate1 );
  58.     FreeRing0Callgate( callgate2 );
  59.  
  60.     DeleteSharedMemoryRegion();
  61.  
  62.     return 0;
  63. }
  64.  
  65. void ShowPhysicalPages(void)
  66. {
  67.     DWORD linearAddr;
  68.     MEMORY_BASIC_INFORMATION mbi;
  69.  
  70.     //
  71.     // Get the address of a 16 bit DLL that's below 1MB (KRNL386's DGROUP)
  72.     //
  73.     linearAddr = Get_KRNL386_DGROUP_LinearAddress();
  74.     printf( "KRNL386 DGROUP      - Linear:%08X  Physical:%08X  %s\n",
  75.             linearAddr,
  76.             GetPhysicalAddrFromLinear(linearAddr),
  77.             GetPageAttributesAsString(linearAddr) );
  78.  
  79.     //
  80.     // Get the starting address of the code area.  We'll pass VirtualQuery
  81.     // the address of a routine within the code area.
  82.     //
  83.     VirtualQuery( ShowPhysicalPages, &mbi, sizeof(mbi) );
  84.     linearAddr = (DWORD)mbi.BaseAddress;
  85.     printf( "First code page     - Linear:%08X  Physical:%08X  %s\n",
  86.             linearAddr,
  87.             GetPhysicalAddrFromLinear(linearAddr),
  88.             GetPageAttributesAsString(linearAddr) );
  89.  
  90.     //
  91.     // Get the starting address of the data area.  We'll pass VirtualQuery
  92.     // the address of a global variable within the data area.
  93.     //
  94.     VirtualQuery( &callgate1, &mbi, sizeof(mbi) );
  95.     linearAddr = (DWORD)mbi.BaseAddress;
  96.     printf( "First data page     - Linear:%08X  Physical:%08X  %s\n",
  97.             linearAddr,
  98.             GetPhysicalAddrFromLinear(linearAddr),
  99.             GetPageAttributesAsString(linearAddr) );
  100.  
  101.     //
  102.     // Get the address of a data section with the SHARED attribute
  103.     //
  104.     MySharedSectionVariable = 1;    // Touch it to force it present
  105.     linearAddr = (DWORD)&MySharedSectionVariable;
  106.     printf( "Shared section      - Linear:%08X  Physical:%08X  %s\n",
  107.             linearAddr,
  108.             GetPhysicalAddrFromLinear(linearAddr),
  109.             GetPageAttributesAsString(linearAddr) );
  110.  
  111.     //
  112.     // Get the address of a resource within the module
  113.     //
  114.     linearAddr = (DWORD)
  115.             FindResource(GetModuleHandle(0), MAKEINTATOM(1), RT_STRING);
  116.     printf( "Resources           - Linear:%08X  Physical:%08X  %s\n",
  117.             linearAddr,
  118.             GetPhysicalAddrFromLinear(linearAddr),
  119.             GetPageAttributesAsString(linearAddr) );
  120.     
  121.     //
  122.     // Get the starting address of the process heap area.
  123.     //
  124.     linearAddr = (DWORD)GetProcessHeap();
  125.     printf( "Process Heap        - Linear:%08X  Physical:%08X  %s\n",
  126.             linearAddr,
  127.             GetPhysicalAddrFromLinear(linearAddr),
  128.             GetPageAttributesAsString(linearAddr) );
  129.  
  130.     //
  131.     // Get the starting address of the process environment area.
  132.     //
  133.     VirtualQuery( GetEnvironmentStrings(), &mbi, sizeof(mbi) );
  134.     linearAddr = (DWORD)mbi.BaseAddress;
  135.     printf( "Environment area    - Linear:%08X  Physical:%08X  %s\n",
  136.             linearAddr,
  137.             GetPhysicalAddrFromLinear(linearAddr),
  138.             GetPageAttributesAsString(linearAddr) );
  139.  
  140.     //
  141.     // Get the starting address of the stack area.  We'll pass
  142.     // the address of a stack variable to VirtualQuery
  143.     //
  144.     VirtualQuery( &linearAddr, &mbi, sizeof(mbi) );
  145.     linearAddr = (DWORD)mbi.BaseAddress;
  146.     printf( "Current Stack page  - Linear:%08X  Physical:%08X  %s\n",
  147.             linearAddr,
  148.             GetPhysicalAddrFromLinear(linearAddr),
  149.             GetPageAttributesAsString(linearAddr) );
  150.  
  151.     //
  152.     // Show the address of a memory mapped file
  153.     //
  154.     linearAddr = (DWORD)PMemMapFileRegion;
  155.     printf( "Memory Mapped file  - Linear:%08X  Physical:%08X  %s\n",
  156.             linearAddr,
  157.             GetPhysicalAddrFromLinear(linearAddr),
  158.             GetPageAttributesAsString(linearAddr) );
  159.  
  160.     //
  161.     // Show the address of a routine in KERNEL32.DLL
  162.     //
  163.     linearAddr = (DWORD)
  164.         GetProcAddress( GetModuleHandle("KERNEL32.DLL"), "VirtualQuery" );
  165.     printf( "KERNEL32.DLL        - Linear:%08X  Physical:%08X  %s\n",
  166.             linearAddr,
  167.             GetPhysicalAddrFromLinear(linearAddr),
  168.             GetPageAttributesAsString(linearAddr) );
  169. }
  170.  
  171. HANDLE HFileMapping;
  172.  
  173. void CreateSharedMemoryRegion(void)
  174. {
  175.     BYTE myByte;
  176.     
  177.     HFileMapping = CreateFileMapping( (HANDLE)0xFFFFFFFF,   // File handle
  178.                                     0,                      // security
  179.                                     PAGE_READWRITE,         // protection
  180.                                     0, 0x1000,              // size
  181.                                     "MyFileMapping" );
  182.  
  183.     // In the above call, we can pass PAGE_WRITECOPY instead of
  184.     // PAGE_READWRITE, but then the subsequent MapViewOfFile will fail
  185.  
  186.     if ( !HFileMapping )
  187.     {
  188.         printf("Couldn't create file mapping!\n");
  189.         return;
  190.     }
  191.  
  192.     if ( GetLastError() == ERROR_ALREADY_EXISTS )
  193.         FirstInstance = FALSE;
  194.  
  195.     PMemMapFileRegion = MapViewOfFile( HFileMapping,            // hMapObject
  196.                                         FILE_MAP_ALL_ACCESS,    // access
  197.                                         0, 0,                   // offset
  198.                                         0 );                    // size
  199.     if ( !PMemMapFileRegion )
  200.     {
  201.         printf("Couldn't map view of file!\n");
  202.         return;
  203.     }
  204.     
  205.     myByte = *PMemMapFileRegion;    // Touch the memory to force it present
  206. }
  207.  
  208. void DeleteSharedMemoryRegion(void)
  209. {
  210.     if ( PMemMapFileRegion )
  211.         UnmapViewOfFile( PMemMapFileRegion );
  212.     
  213.     if ( HFileMapping )
  214.         CloseHandle( HFileMapping );
  215. }
  216.  
  217. void Dummy(void)    // Exists solely for us to bash
  218. {
  219. }
  220.  
  221. void ModifyCodePage(void)
  222. {
  223.     BYTE srcByte = 0xCC;
  224.     DWORD cbWritten;
  225.     
  226.     if ( !WriteProcessMemory(GetCurrentProcess(), Dummy, &srcByte,
  227.                                 1, &cbWritten) || (cbWritten != 1) )
  228.         printf("Couldn't write to code page!\n");
  229. }
  230.  
  231. DWORD Get_KRNL386_DGROUP_LinearAddress(void)
  232. {
  233.     HMODULE hModKRNL386;
  234.     LDT_ENTRY selectorEntry;
  235.     
  236.     hModKRNL386 = LoadLibrary16("KRNL386.EXE");
  237.     if ( hModKRNL386 < (HMODULE)0x20 )
  238.     {
  239.         printf("Couldn't get selector of KRNL386 DGROUP");
  240.         return 0;
  241.     }
  242.  
  243.     if ( !GetThreadSelectorEntry( GetCurrentThread(), (DWORD)hModKRNL386, 
  244.                                     &selectorEntry ) )
  245.     {
  246.         printf("GetThreadSelectorEntry failed!");
  247.         return 0;
  248.     }
  249.  
  250.     FreeLibrary16( hModKRNL386 );
  251.  
  252.     return  (selectorEntry.HighWord.Bytes.BaseHi << 24)
  253.             + (selectorEntry.HighWord.Bytes.BaseMid << 16)
  254.             + selectorEntry.BaseLow;
  255. }
  256.  
  257. DWORD GetPhysicalAddrFromLinear(DWORD linear)
  258. {
  259.     if ( !callgate1 )
  260.         callgate1 = GetRing0Callgate( (DWORD)_GetPhysicalAddrFromLinear, 1 );
  261.     
  262.     if ( callgate1 )
  263.     {
  264.         WORD myFwordPtr[3];
  265.         
  266.         myFwordPtr[2] = callgate1;
  267.         __asm   push    [linear]
  268.         __asm   cli
  269.         __asm   call    fword ptr [myFwordPtr]
  270.         __asm   sti
  271.  
  272.         // The return value is in EAX.  The compiler will complain, but...
  273.     }
  274.     else
  275.         return 0xFFFFFFFF;
  276. }
  277.  
  278. DWORD GetPageAttributes(DWORD linear)
  279. {
  280.     if ( !callgate2 )
  281.         callgate2 = GetRing0Callgate( (DWORD)_GetPageAttributes, 1 );
  282.     
  283.     if ( callgate2 )
  284.     {
  285.         WORD myFwordPtr[3];
  286.         
  287.         myFwordPtr[2] = callgate2;
  288.         __asm   push    [linear]
  289.         __asm   cli
  290.         __asm   call    fword ptr [myFwordPtr]
  291.         __asm   sti
  292.  
  293.         // The return value is in EAX.  The compiler will complain, but...
  294.     }
  295.     else
  296.         return 0xFFFFFFFF;
  297. }
  298.  
  299. PSTR GetPageAttributesAsString(DWORD linear)
  300. {
  301.     DWORD attr;
  302.     static char szRetBuffer[128];
  303.     
  304.     attr = GetPageAttributes(linear);
  305.     if ( (attr & 1) == 0  )
  306.         return "not present";
  307.     
  308.     strcpy( szRetBuffer, attr & 2 ? "Read/Write" : "ReadOnly" );
  309.     strcat( szRetBuffer, " " );
  310.     strcat( szRetBuffer, attr & 4 ? "USER" : "SPRVSR" );
  311.     
  312.     return szRetBuffer;
  313. }
  314.  
  315.